package script.login
{
	import eventUtil.EventCenter;
	
	import laya.components.Script;
	import laya.display.Input;
	import laya.events.Event;
	import laya.media.SoundManager;
	
	import model.HttpErrorCode;
	import model.HttpRequestUtil;
	import model.Userdata;
	
	import script.ViewManager;
	import script.baseView.BaseViewController;
	
	import ui.login.LoginPanelUI;
	
	import utils.UtilTool;
	
	public class LoginController extends BaseViewController
	{
		protected var uisKin:LoginPanelUI;

		
		public function LoginController()
		{
			super();
		}
		
		override public function onStart():void
		{
			uisKin = this.owner as LoginPanelUI;
			
			uisKin.closebtn .on(Event.CLICK,this,onCloseScene);
			//uisKin.bgimg.alpha = 0.95;
			

			
			
			uisKin.input_account.maxChars = 11;
			uisKin.input_account.restrict = "0-9";
			
			uisKin.input_pwd.maxChars = 20;
			uisKin.input_pwd.type = Input.TYPE_PASSWORD;
			
			
			uisKin.txt_reg.underline = true;
			uisKin.txt_reg.underlineColor =  "#121212";
			
			uisKin.txt_forget.underline = true;
			uisKin.txt_forget.underlineColor =  "#121212";
			
			uisKin.txt_reg.on(Event.CLICK,this,onRegister);
			uisKin.txt_forget.on(Event.CLICK,this,onResetpwd);
			
			uisKin.loginbtn.on(Event.CLICK,this,onLogin);
			
			//uisKin.input_account.on(Event.KEY_DOWN,this,onAccountKeyUp);
			
			//uisKin.input_pwd.on(Event.KEY_DOWN,this,onAccountKeyUp);
			
			uisKin.input_account.focus = true;
			
			//var param:String = "loginname=13564113173" + "&pwd=123456";
			//HttpRequestUtil.instance.Request(HttpRequestUtil.httpUrl + HttpRequestUtil.loginInUrl,this,onLoginBack,param,"post");
			
			//uisKin.loginbtn.on(Event.CLICK,this,onLoginNow);
			loginAccount();
			//ViewManager.instance.openView(ViewManager.VIEW_MAIN_PANEL);

		}
		private function loginAccount():void
		{
			var account:String = UtilTool.getLocalVar("useraccount","0");
			var pwd:String = UtilTool.getLocalVar("userpwd","0");
			if(account != "0" && pwd != "0" && account != "" && pwd != "")
			{
				uisKin.input_account.text = account;
				uisKin.input_pwd.text = pwd;
				
				var param:String = "loginname=" + account + "&pwd=" + pwd ;
				HttpRequestUtil.instance.Request(HttpRequestUtil.httpUrl + HttpRequestUtil.loginInUrl,this,onLoginBack,param,"post");
			}
			
		}
	
		private function onLogin():void
		{
			// TODO Auto Generated method stub
			if(uisKin.input_account.text.length != 11)
			{
				ViewManager.showAlert("请填写正确的账号");
				return;
			}
			if(uisKin.input_pwd.text.length < 6)
			{
				ViewManager.showAlert("密码位数至少是6位");
				return;
			}
			
			var param:String = "loginname=" + uisKin.input_account.text + "&pwd=" + uisKin.input_pwd.text;
			HttpRequestUtil.instance.Request(HttpRequestUtil.httpUrl + HttpRequestUtil.loginInUrl,this,onLoginBack,param,"post");
		}
		
		private function onLoginBack(data:Object):void
		{
			var result:Object = JSON.parse(data as String);
			if(result.code == HttpErrorCode.OK)
			{
				Userdata.instance.isLogin = true;
				Userdata.instance.userAccount = result.phonenum;
				
				

				Userdata.instance.userName = result.rolename;
				Userdata.instance.regdate = result.regdate;

				Userdata.instance.address = result.addressDetail != null?result.addressDetail:"";

				//Userdata.instance.accountType = result.usertype;
				
				//ViewManager.showAlert("登陆成功");
				//EventCenter.instance.event(EventCenter.LOGIN_SUCESS, uisKin.input_account.text);
				UtilTool.setLocalVar("useraccount",uisKin.input_account.text);
				UtilTool.setLocalVar("userpwd",uisKin.input_pwd.text);
				
				Userdata.instance.loginTime = (new Date()).getTime();
				UtilTool.setLocalVar("loginTime",Userdata.instance.loginTime);
				
				ViewManager.instance.closeView(ViewManager.VIEW_LOGIN_PANEL);
				ViewManager.instance.openView(ViewManager.VIEW_MAIN_PANEL);
				
				//console.log(Browser.document.cookie.split("; "));
			}
			
		}
		private function onRegister():void
		{
			// TODO Auto Generated method stub
			ViewManager.instance.openView(ViewManager.VIEW_REGISTER_PANEL,true);
			
		}
		
		private function onResetpwd():void
		{
			//ViewManager.instance.openView(ViewManager.VIEW_CHANGEPWD,false);
			
		}
		private function onCloseScene():void
		{
			// TODO Auto Generated method stub
			//Scene.close("login/LogPanel.scene");
			
			ViewManager.instance.closeView(ViewManager.VIEW_LOGIN_PANEL);
			ViewManager.instance.openView(ViewManager.VIEW_MAIN_PANEL);
		}	
	}
}